package com.jstyle.jclife.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.daoManager.UserInfoDaoManager;
import com.jstyle.jclife.model.UserInfo;
import com.jstyle.jclife.utils.GlobalConst;
import com.jstyle.jclife.utils.ImageUtils;
import com.jstyle.jclife.view.HeadCircleView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/11.
 */

public class SettingAdapter extends RecyclerViewBaseAdapter<String> {


    final int VIEWTYPE_NORMAL = 0;
    final int VIEWTYPE_OTHER = 1;
    final int VIEWTYPE_ICON = 2;
    final int VIEWTYPE_exit =3;

    private String battery;
    private boolean needUpdate;

    public SettingAdapter(List<String> mDatas) {
        super(mDatas);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = null;
        switch (viewType) {
            case VIEWTYPE_NORMAL:
                View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new NormalViewHolder(view);
                break;
            case VIEWTYPE_OTHER:
                View viewOther = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new OtherViewHolder(viewOther);
                break;
            case VIEWTYPE_ICON:
                View viewIcon = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new IconViewHolder(viewIcon);
                break;
            case VIEWTYPE_exit:
                View viewExit = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new ExitViewHolder(viewExit);
                break;
        }
        return baseViewHolder;
    }

    public class OtherViewHolder extends BaseViewHolder {

        public OtherViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public class IconViewHolder extends BaseViewHolder {

        @BindView(R.id.iv_setting_icon)
        ImageView iv_icon;
        @BindView(R.id.bt_setting_name)
        Button btSettingRight;

        public IconViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class NormalViewHolder extends BaseViewHolder {
        @BindView(R.id.bt_setting_position)
        Button btSettingPosition;
        @BindView(R.id.view_space)
        View viewSpace;
        @BindView(R.id.bt_setting_right)
        Button btSettingRight;
        @BindView(R.id.view_bottom)
        View viewBottom;
        public NormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public class ExitViewHolder extends BaseViewHolder {
        @BindView(R.id.bt_exit)
        Button exit;

        public ExitViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    protected void bindData(BaseViewHolder holder, final int position) {

        switch (holder.getItemViewType()) {
            case VIEWTYPE_NORMAL:
                bindNormal(holder, position);
                break;
            case VIEWTYPE_ICON:
                bindIcon(holder);
                break;
            case VIEWTYPE_exit:
                bindExit(holder);
                break;
        }

    }

    private void bindExit(BaseViewHolder holder) {
    }

    private void bindIcon(BaseViewHolder holder) {
        IconViewHolder iconViewHolder = (IconViewHolder) holder;
        Context context = iconViewHolder.itemView.getContext();
        UserInfo userInfo= UserInfoDaoManager.getUserByUid(NetWorkUtil.getUserId());
        if(userInfo==null)return;
        Uri iconUri = ImageUtils.getIconUri(context, userInfo.getUserId());
        String userName=userInfo.getName();
        Bitmap bmHead = ImageUtils.findHeadBitmap(context, iconUri);
        bmHead = bmHead == null ? BitmapFactory.decodeResource(context.getResources(),
                R.drawable.user_icon) : bmHead;
        HeadCircleView circleView = new HeadCircleView(bmHead);
        iconViewHolder.iv_icon.setImageDrawable(circleView);
        if (TextUtils.isEmpty(userName))userName=GlobalConst.KEY_DefaultName;
        iconViewHolder.btSettingRight.setText(userName);
    }

    private void bindNormal(BaseViewHolder holder, final int position) {
        NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
        normalViewHolder.btSettingPosition.setText(mDataList.get(position));
        if (position == 9 || position == 12) {
            normalViewHolder.viewSpace.setVisibility(View.INVISIBLE);
        }else{
            normalViewHolder.viewSpace.setVisibility(View.VISIBLE);
        }
        normalViewHolder.btSettingRight.setText("");
        if (position == 1 || position == 2||position==8) {
            normalViewHolder.btSettingRight.setVisibility(View.VISIBLE);
        }else{
            normalViewHolder.btSettingRight.setVisibility(View.INVISIBLE);
        }

        if(position==2&&!TextUtils.isEmpty(battery)) {
            normalViewHolder.btSettingRight.setText(battery);
            normalViewHolder.btSettingRight.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
        }else{
            normalViewHolder.btSettingRight.setGravity(Gravity.CENTER);
        }
        Context context=((NormalViewHolder) holder).itemView.getContext();
        if(position==8&&needUpdate) {
            normalViewHolder.btSettingRight.setText(context.getString(R.string.New_Version_Found));
            normalViewHolder.btSettingRight.setTextColor(Color.RED);
        }else{
            normalViewHolder.btSettingRight.setTextColor(context.getResources().getColor(R.color.style_text_color));
        }
        normalViewHolder.viewBottom.setVisibility(position==mDataList.size()-1?View.VISIBLE:View.GONE);
        setDrawable(holder.itemView.getContext(), position-1, normalViewHolder.btSettingPosition);
    }

    private void setDrawable(Context context, int position, Button button) {
        int id = 0;
        switch (position) {
            case 0:
                id = R.drawable.user_device;
                break;
            case 1:
                id = R.drawable.user_battery;
                break;
            case 2:
                id = R.drawable.user_info;
                break;
            case 3:
                id = R.drawable.user_goal;
                break;
            case 4:
                id = R.drawable.user_health;
                break;
            case 5:
                id = R.drawable.user_setting_bp;
                break;
            case 6:
                id = R.drawable.user_device_setting;
                break;
            case 7:
                id = R.drawable.user_ota;
                break;
            case 8:
                id = R.drawable.user_factorymode;
                break;
            case 9:
                id = R.drawable.user_aboutus;
                break;
            case 10:
                id = R.drawable.user_aboutus;
                break;
            case 11:
                id = R.drawable.user_advice;
                break;
        }
        Drawable drawable = context.getResources().getDrawable(id);
        Drawable drawableRight = context.getResources().getDrawable(R.drawable.user_right);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            drawableRight.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            button.setCompoundDrawables(drawable, null, position==1?null:drawableRight, null);
        }
    }

    @Override
    public int getItemViewType(int position) {
        int type = VIEWTYPE_NORMAL;
        switch (position) {
            case 0:
                type=VIEWTYPE_ICON;
                break;
            case 10:
                type = VIEWTYPE_OTHER;
                break;
            case 13:
                type = VIEWTYPE_exit;
                break;
        }
        return type;
    }

    /**
     * @param viewType
     * @return
     */
    @Override
    protected int getLayoutId(int viewType) {
        int id = R.layout.item_setting;
        switch (viewType) {
            case VIEWTYPE_NORMAL:
                id = R.layout.item_setting;
                break;
            case VIEWTYPE_OTHER:
                id = R.layout.item_other;
                break;
            case VIEWTYPE_ICON:
                id=R.layout.item_setting_icon;
                break;
            case VIEWTYPE_exit:
                id=R.layout.item_exit_login;
                break;
        }
        return id;
    }
    public void setBattery(String battery){
        this.battery=battery;
        notifyDataSetChanged();
    }
    public int getBattery(){
        int batteryValue=0;
        if(!TextUtils.isEmpty(battery)){
            String[]batterys=battery.split("%");
            batteryValue=Integer.valueOf(batterys[0].trim());
        }

       return batteryValue;
    }
    public void setNeedUpdate(boolean needUpdate){
        this.needUpdate=needUpdate;
        notifyDataSetChanged();
    }
    public void setActivate(boolean isActivate) {
        //this.isActivate = isActivate;
       // notifyDataSetChanged();
    }
}
